package zdream.utils.math;

/**
 * @author Zdream
 * @date 2023-12-23
 * @since 0.0.1
 */
public class Rectangle {
	public float x, y, width, height;

	public Rectangle() {
	}

	public Rectangle(float x, float y, float width, float height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}

	public Rectangle(Rectangle rect) {
		x = rect.x;
		y = rect.y;
		width = rect.width;
		height = rect.height;
	}

	public Rectangle withX(float x) {
		this.x = x;
		return this;
	}

	public Rectangle withY(float y) {
		this.y = y;
		return this;
	}

	public Rectangle withWidth(float width) {
		this.width = width;
		return this;
	}

	public Rectangle withHeight(float height) {
		this.height = height;
		return this;
	}

	public float left() {
		return x;
	}

	public float right() {
		return x + width;
	}

	public float top() {
		return y + height;
	}

	public float bottom() {
		return y;
	}

	public float centerX() {
		return x + width / 2;
	}

	public float centerY() {
		return y + height / 2;
	}

	public void set(float x, float y, float width, float height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}

	public boolean contains(float x, float y) {
		return this.x <= x && this.x + this.width >= x && this.y <= y && this.y + this.height >= y;
	}

	public boolean containsWithoutEdge(float x, float y) {
		return this.x < x && this.x + this.width > x && this.y < y && this.y + this.height > y;
	}

	public boolean contains(Circle circle) {
		return (circle.x - circle.radius >= x) && (circle.x + circle.radius <= x + width)
			&& (circle.y - circle.radius >= y) && (circle.y + circle.radius <= y + height);
	}

	public boolean containsWithoutEdge(Circle circle) {
		return (circle.x - circle.radius > x) && (circle.x + circle.radius < x + width)
			&& (circle.y - circle.radius > y) && (circle.y + circle.radius < y + height);
	}

	public boolean overlaps(Rectangle r) {
		return x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y;
	}
}
